package org.hujunfeng.http;

import org.hujunfeng.http.BuffReader;
import org.hujunfeng.http.URLTool;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 胡俊丰
 * @Date 2024/10/4
 *
 * 封装HTTP请求对象
 **/
public class HttpRequest {

    /**
     * 请求方法
     */
    private String method;

    /**
     * 资源路径
     */
    private String resource;

    /**
     * 查询参数
     */
    private String queryString;

    /**
     * 协议版本
     */
    private String version;

    /**
     * 请求头部
     */
    private Map<String,String> headers;

    /**
     * 输入流
     */
    private BuffReader input;

    /**
     * 读取字符集
     */
    private String charset = "UTF-8";

    public HttpRequest(InputStream input){
        this.input = new BuffReader(input);
        headers = new HashMap<String, String>();
    }

    /**
     * 解析请求
     */
    public void parseRequest() throws IOException{
        // 读取首行
        String line = null;
        
            line = input.readLine("\r\n", "UTF-8");

            String[] requestLine = line.split(" ");

            if (requestLine != null && requestLine.length == 3){
                if (requestLine[0] != null){
                    this.method = requestLine[0].toLowerCase();
                }

                if (requestLine[1] != null){
                    this.resource = requestLine[1];

                    if (!resource.equals("")){
                        // 解码URL路径
                        resource = URLTool.decode(resource);

                        String temp = resource;
                        int index = temp.indexOf("?");

                        if (index > -1){
                            resource = resource.substring(0, index);
                            if (temp.length() > index + 1){
                                queryString = temp.substring(index + 1);
                            }
                        }
                    }
                }

                if (requestLine[2] != null){
                    this.version = requestLine[2];
                }
            }

            while (true){
                line = input.readLine("\r\n", "UTF-8");

                // 如果等于空字符，遇到空行，跳出循环
                if (line.equals("")){
                    break;
                }

                String[] heads = line.split(":");
                if (heads != null && heads.length == 2){
                    if (heads[0] != null && heads[1] != null){
                        this.headers.put(heads[0].toLowerCase(), heads[1].trim());
                    }
                }
            }
    }

    /**
     * 获取请求头
     * @param key
     * @return
     */
    public String getHeader(String key){
        return headers.get(key.toLowerCase());
    }

    /**
     * 获取对请求体的阅读器
     * @return
     */
    public Reader getReader() throws IOException{
        return new InputStreamReader(input, charset);
    }

    /**
     * 获取对请求体的输入流
     * @return
     */
    public InputStream getInput(){
        return this.input;
    }

    /**
     * 设置字符集
     * @param charset
     */
    public void setCharset(String charset){
        this.charset = charset;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getResource() {
        return resource;
    }

    public void setResource(String resource) {
        this.resource = resource;
    }

    public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}
